package kr.co.exight.onApp;

import java.util.List;

import com.google.android.gcm.GCMRegistrar;

import library.ExPreferenceManager;
import library.GCMHttpConnect;
import library.Library;
import library.Variables;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;

public class ActivityPreference extends Activity implements OnClickListener, OnCheckedChangeListener {

	private CheckBox checkBox;
	private ExPreferenceManager preManager;
	private static final String SENDER_ID = "242049221661";	

	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    
	    setContentView(R.layout.act_preference);
	    //LinearLayout ll = (LinearLayout)findViewById(R.id.setting_llNotice)
	    findViewById(R.id.setting_llNotice).setOnClickListener(this);
	    findViewById(R.id.setting_btnExit).setOnClickListener(this);
	    findViewById(R.id.setting_btnDevelop).setOnClickListener(this);
	    checkBox = (CheckBox)findViewById(R.id.setting_checkboxAlert);
	    preManager = new ExPreferenceManager(this);
	  
	    checkBox.setChecked(true);
	    if(preManager.getItem("KeyRegister").equals("")){
	    	checkBox.setChecked(false);
	    }
	    checkBox.setOnCheckedChangeListener(this);
	}
	
	
	
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
		
		if(isChecked){
			final String regId = GCMRegistrar.getRegistrationId(ActivityPreference.this);
			
			if (regId.equals("") || regId == null) {
	      		GCMRegistrar.register(this, SENDER_ID);
				
	      	}else {
	      		if(preManager.getItem("KeyRegister").equals("")){
	      			GCMHttpConnect httpConnect = new GCMHttpConnect(this, Variables.NOTIFICATION_ADD_URL +  regId +
	      			"/add", 0);
	      			httpConnect.start();
	      		}
	      	}
			
		}else { // 체크 풀렸을때에
			final String regId = GCMRegistrar.getRegistrationId(ActivityPreference.this);
			if (regId.equals("") || regId == null) {
	      		//GCMRegistrar.register(this, SENDER_ID);
				
	      	}else {
	      		if(preManager.getItem("KeyRegister").equals("true")){
	      			GCMHttpConnect httpConnect = new GCMHttpConnect(this, Variables.NOTIFICATION_ADD_URL +  regId +
	      			"/delete", 1);
	      			httpConnect.start();
	      		}
	      	}
		}
	}



	public void requestKillProcess(final Context context) {

		int sdkVersion = Integer.parseInt(Build.VERSION.SDK);

		if (sdkVersion < 8) {
			
		} else {
			new Thread(new Runnable() {
				public void run() {
					ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
					String name = getApplicationInfo().processName;

					while (true) {
						List<RunningAppProcessInfo> list = am
								.getRunningAppProcesses();

						for (RunningAppProcessInfo i : list) {		
							if (i.processName.equals(name) == true) {

								if (i.importance >= RunningAppProcessInfo.IMPORTANCE_VISIBLE)
									am.restartPackage(getPackageName());

								Thread.yield();					
								break;
							}
						}
					}
				}

			}, "Process Killer").start();
		}
	}


	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.setting_btnExit:
			ExPreferenceManager a = new ExPreferenceManager(ActivityPreference.this);
			a.putItem(ExPreferenceManager.TAG_AUTOLOGIN, "");
			a.putItem(ExPreferenceManager.TAG_TIMETABLE_WRITE, "");
        	Intent exit_intent = new Intent(this, ActivityCertificate.class);
 
        	startActivity(exit_intent);
        	finish();
        	ActivityMain.mainActivity.finish();
        	
			break;
			
		case R.id.setting_llNotice:
			Intent i1 = new Intent(ActivityPreference.this, ActivityPreferNotice.class);
			startActivity(i1);
			break;
			
		case R.id.setting_btnDevelop:

			Intent i2 = new Intent(ActivityPreference.this, ActivityDeveloper.class);
			startActivity(i2);
			
			break;
		}
	}

}
